package com.jintian.smart.kernel.autoconfigure.model;

import com.jintian.smart.kernel.autoconfigure.beetl.BeetlSqlBeanDefinitionRegistrar;
import jakarta.annotation.Resource;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.beetl.ext.spring6.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring6.BeetlSpringView;
import org.beetl.ext.spring6.BeetlSpringViewResolver;
import org.beetl.sql.core.ExecuteContext;
import org.beetl.sql.ext.Slf4JLogInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * beetl - master 创建并配置SQLMangger
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = "beetl.enabled", havingValue = "true", matchIfMissing = true)
@Import({BeetlSqlBeanDefinitionRegistrar.class})
public class BeetlCustomizeConfig {
    @Value("${beetlsql.log.showLineNum:true}")
    private boolean showLineNum;

    @Value("${beetlsql.log.preferredShowClass:#{null}}")
    private String preferredShowClass;

    @Value("${beetl.suffix:html}")
    private String suffix;

    @Value("${smart.multiContext:true}")
    private boolean multiContext;

    @Resource(name = "beetlConfig")
    private BeetlGroupUtilConfiguration beetlGroupUtilConfiguration;

    /**
     * 日志拦截器
     */
    @Bean
    @ConditionalOnMissingBean(Slf4JLogInterceptor.class)
    Slf4JLogInterceptor consoleDebugInterceptor() {
        return new Slf4JLogInterceptor(this.showLineNum, this.preferredShowClass);
    }

    /**
     * sql.md中临时开启debug
     */
    @Bean
    Function debugSwitchFunction() {
        Function fn = new Function() {
            @Override
            public Object call(Object[] paras, Context ctx) {
                ExecuteContext context = (ExecuteContext) ctx.getGlobal(ExecuteContext.NAME);
                context.setContextPara("debug", true);
                return null;
            }
        };
        return fn;
    }

    /**
     * beetl springmvc View 解析器
     */
    @Bean(name = "beetlViewResolver")
    @ConditionalOnMissingBean(BeetlSpringViewResolver.class)
    public BeetlSpringViewResolver getBeetlSpringViewResolver(ApplicationContext applicationContext) {
        BeetlSpringViewResolver beetlSpringViewResolver = multiContext
                ? new MutiContextBeetlSpringViewResolver(applicationContext) : new BeetlSpringViewResolver();
        beetlSpringViewResolver.setViewClass(BeetlSpringView.class);
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        // ajax 支持
        beetlSpringViewResolver.setViewNames("*." + suffix, "*." + suffix + "#*");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }
}
